package com.mandroid.forecast.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class SettingActivity extends Activity{
	
	private static final int MENU_ABOUT=Menu.FIRST;
	
	private static final int MENU_QUIT=Menu.FIRST + 1;
	
	private static final String UNIT_PREFS="Unit_Prefs";
	
	private static final String UNIT="unit";
	
//	private static final String YOU_LOCATION="you_location";
	
	RadioButton metricButton ;
	
	RadioButton britishButton;
	
	RadioGroup unitGroup;
	
	Button saveButton;

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting);
		init();
		SharedPreferences prefs = getSharedPreferences(UNIT_PREFS, 0);
		int unit = prefs.getInt(UNIT, 0);
		if(unit == 0) {
			prefs.edit().putInt(UNIT, unit);
			unitGroup.check(R.id.british_unit);
		} else {
			unitGroup.check(R.id.metric_unit);
		}
	}
	
	private void init(){
		metricButton = (RadioButton)findViewById(R.id.metric_unit);
		britishButton = (RadioButton)findViewById(R.id.british_unit);
		saveButton = (Button)findViewById(R.id.save_setting);
		unitGroup = (RadioGroup)findViewById(R.id.unit_group);
		
		saveButton.setOnClickListener(saveSettingListener);
	}
	
    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
    	menu.add(0, MENU_ABOUT, 0, "About");
    	menu.add(0, MENU_QUIT, 0, "Quit");
    	return true;
	}
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
    	switch (item.getItemId()) {
    	case MENU_ABOUT:
    		openOptionsDialog();
    		break;
    	case MENU_QUIT:
    		finish();
    		break;
    	default:
    		break;
    	}
        return true;
    }
    
    private void openOptionsDialog() {
    	new AlertDialog.Builder(this)
    		.setTitle(R.string.about_title)
    		.setMessage(R.string.about_message)
    		.setPositiveButton(R.string.about_ok,
    				new DialogInterface.OnClickListener(){
    					public void onClick(
    						DialogInterface dialoginterface, int i){
    						}
    				})
    		.setNegativeButton(R.string.about_homepage,
    				new DialogInterface.OnClickListener(){
    					public void onClick(
    							DialogInterface dialoginterface, int i){
    								Uri uri = Uri.parse("http://maleoapp.appspot.com/");
    								Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    								startActivity(intent);
    							}
    					}).show();

    }
    
    private OnClickListener saveSettingListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			int checked = unitGroup.getCheckedRadioButtonId();
			int pref = 0;
			if(checked == R.id.metric_unit) {
				pref = 1;
			}
			SharedPreferences prefs = getSharedPreferences(UNIT_PREFS, 0);
			prefs.edit().putInt(UNIT, pref).commit();
			Intent intent = new Intent(SettingActivity.this, ForecastActivity.class);
			startActivity(intent);
		}
    	
    };

}
